import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { Router, createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

nprogress.configure({ showSpinner: false })
/**
 * 自动导入静态路由，无需手动倒入
 * import.meta.glob  用于匹配需要导入的模块
 * vite中import.meta.glob用法: https://blog.csdn.net/weixin_45172119/article/details/129233197
 */

//Record定义一个对象key、value类型
const modules: Record<string, any> = import.meta.glob(['./modules/**/*.ts'], {
  eager: true,
})

// 初始化路由信息
let routes: Array<RouteRecordRaw> = []

Object.keys(modules).forEach((path) => {
  routes.push(modules[path].default)
})
routes = routes.flat(1)

/** 原始静态路由 */
export const constantRoutes: Array<RouteRecordRaw> = []

// Vue Router 不再是一个类，而是一组函数。现在你不用再写 new Router()，而是要调用 createRouter
//创建路由实例
export const router: Router = createRouter({
  history: createWebHistory(''), //接收base,base?: string | undefined
  routes: routes,
})

router.beforeEach((to, from, next) => {
  nprogress.start()
  next()
})

router.afterEach(() => {
  nprogress.done()
})
export default router
